import re
import math

#open the spawn trigs, which will be our templare

w = open("C:\Python27\demon_lore\spawn_trig2.txt", 'r')
r = open("income_trig.txt", 'r')

itemplate = r.read()

r.close()

#read in the 5 triggers as raw text
template = w.read()

#a list of usable death counters
dcs = ['Alan Turret', 'Cantina', 'Cave', 'Cave-in', 'Disruption Field', 'Duke Turret type 1',
       'Duke Turret type 2', 'Goliath Turret', 'Independent Command Center', 'Independent Starport', 'Jump Gate',
       'Khaydarin Crystal Formation', 'Kyadarin Crystal Formation', 'Mining Platform']


w.close()

MAX_UNITS = 150 #a global parameter for the most units a player can control

#special line that must be added for each additional player
#used to keep locks working correctly
DEATHS = '\n\tDeaths("Player 1", "Alan Turret", At most, 0);'



#regular expressions for replacing parameters
GET_DC = re.compile(r'Alan Turret')
GET_PLAYER = re.compile(r'Player 1')
GET_BUILD = re.compile(r'Terran Barracks')
GET_UNIT = re.compile(r'Fenix \(Zealot\)')
GET_MAX_U = re.compile(r'(?<= )100(?=\))')
GET_MAX_U_MOST = re.compile(r'(?<= )101(?=\))')
GET_MAX = re.compile(r'(?<= )150(?=\))')
GET_MAX_MOST = re.compile(r'(?<= )151(?=\))')
GET_COUNT = re.compile(r'(?<= )2(?=,)')
GET_COST = re.compile(r'50(?=. ore)')
GET_COST_MOST = re.compile(r'49(?=.? ore)')
#used to insert conditions string
GET_CONDIT = re.compile(r'(?<=Conditions:)')

#


#now our function that outputs raw text triggers given some parameters
def spawn_condit_macro(dc, player):
    condit_string = ""
    for p in range(1, player):
        new_str = DEATHS
        new_str = GET_DC.sub(dc, new_str)
        new_str = GET_PLAYER.sub('Player ' + str(p), new_str)
        condit_string += new_str
    return condit_string


def spawn_macro(build, unit, max_u, count, cost, dc, player):
    """
    Takes several parameters and outputs 4 triggers
    in a single string for acting as a spawn trigger.
    The player number determines the conditions present.

    Args:
        build: a string name for a SC building (e.g. Terran Barracks)
        unit: a string name for a SC unit (e.g. Terran Marine)
        max_u: the maximum number of units of this type at anytime
        count: number of units spawned per building (e.g. 4)
        cost: cost for each building spawn/upkeep (e.g. 50 minerals)
        dc: a string name for the death counter acting as a lock (e.g. Alan Turret)
        player: an integer representing the player number (1-6 inclusive)
    Returns:
        a string representing the four triggers
    """
    new_trigs = template
    #get the DC conditions (will be "" if player = 1)
    condition_string = spawn_condit_macro(dc, player)
    #replace all the parameters with the args
    new_trigs = GET_BUILD.sub(build, new_trigs)
    new_trigs = GET_UNIT.sub(unit, new_trigs)
    new_trigs = GET_MAX_U.sub(str(max_u), new_trigs)
    new_trigs = GET_MAX_U_MOST.sub(str(max_u + 1), new_trigs)
    new_trigs = GET_COUNT.sub(str(count), new_trigs)
    new_trigs = GET_COST.sub(str(cost), new_trigs)
    new_trigs = GET_COST_MOST.sub(str(cost - 1), new_trigs)
    new_trigs = GET_DC.sub(dc, new_trigs)
    new_trigs = GET_MAX.sub(str(MAX_UNITS), new_trigs)
    new_trigs = GET_MAX_MOST.sub(str(MAX_UNITS + 1), new_trigs)
    new_trigs = GET_PLAYER.sub("Player " + str(player), new_trigs)
    #insert the conditions
    new_trigs = GET_CONDIT.sub(condition_string, new_trigs)
    return new_trigs
    
    
    

def income_macro(dc, player):
    """
    """
    new_trigs = itemplate
    #get the DC conditions (will be "" if player = 1)
    condition_string = spawn_condit_macro(dc, player)
    #replace all the parameters with the args
    new_trigs = GET_PLAYER.sub("Player " + str(player), new_trigs)
    #insert the conditions
    new_trigs = GET_CONDIT.sub(condition_string, new_trigs)
    return new_trigs


#the 7 macros

w = open('all_spawns.txt', 'w')

for x in range(1, 7):
    print>>w,spawn_macro('Terran Barracks', 'Fenix (Zealot)', 100, 2, 50, 'Alan Turret', x)
    print>>w,spawn_macro('Terran Engineering Bay', 'Terran Marine', 60, 2, 65, 'Cave', x)
    print>>w,spawn_macro('Terran Academy', 'Terran Ghost', 60, 2, 70, 'Cantina', x)
    print>>w,spawn_macro('Terran Factory', 'Terran Vulture', 36, 2, 80, 'Cave-in', x)
    print>>w,spawn_macro('Terran Armory', 'Terran Goliath', 24, 1, 100, 'Disruption Field', x)
    print>>w,spawn_macro('Terran Starport', 'Terran Firebat', 36, 2, 75, 'Duke Turret type 1', x)
    print>>w,spawn_macro('Terran Science Facility', 'Terran Siege Tank (Tank Mode)', 12, 1, 125, 'Duke Turret type 2', x)

w.close()

    
    
    


